Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve compiler errors that appear due to useDefineForClassFields #241544

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

trevorade
Copy link
Contributor

These changes go a ways towards addressing #186726

These changes DO NOT include setting useDefineForClassFields to true so backsliding is still possible.

These changes were done by hand over a period of about five focused hours. There were 500 errors when I started.

The process I used was:

  • Enable useDefineForClassFields in the base tsconfig file (NOT IN THIS PR)
  • Run npm run watch
  • Identify an error and open the file with an error
  • For any field initialization that references a parameter property:
    • Move the initialization into the constructor
    • Explicitly add the type via a plugin
    • Repeat for any remaining + new errors
  • Look for usages of waitForState and recomputeInitiallyAndOnChange and inline any impacted derived fields

To truly enable useDefineForClassFields, more changes will more than likely need to be made. For now, I was interested in tackling the compilation errors.

All tests pass with these changes.

If the VSCode team would like to take over this PR, that's fine.

The motivation for making these changes is that, at Google, we build this codebase internally. We are going to enable the compilation errors related to useDefineForClassFields in our internal typescript compiler settings (without actually turning on those transpilation changes, similar to microsoft/TypeScript#59623). So we would much prefer to fix these issues in your codebase rather than maintain a huge 200 file patch :P

@trevorade
Copy link
Contributor Author

@microsoft-github-policy-service agree company="Google"

@trevorade
Copy link
Contributor Author

@jrieken, would you be willing to take a look at this?

@jrieken jrieken self-assigned this Feb 25, 2025
@jrieken
Copy link
Member

jrieken commented Feb 25, 2025

@trevorade Thanks for reaching out and starting this. This is a large PR and something we need to coordinate across the team. Usually, we have reservations against PRs that touch very many code owner areas but this seems mostly mechanical and exception-worthy.

I will circle this with the team and see how folks respond. Generally, I am positive about this change and it seems this must happen anyways sooner or later.

To truly enable useDefineForClassFields, more changes will more than likely need to be made

Do you have a feel for what that would be?

@mjbvz You initial concern in #186726 (comment) seems to have fixed, right? Any other insights this that you have?

@trevorade
Copy link
Contributor Author

trevorade commented Feb 25, 2025

@trevorade Thanks for reaching out and starting this. This is a large PR and something we need to coordinate across the team. Usually, we have reservations against PRs that touch very many code owner areas but this seems mostly mechanical and exception-worthy.

I will circle this with the team and see how folks respond. Generally, I am positive about this change and it seems this must happen anyways sooner or later.

Thx. This is exactly what I expected. That seeing such a large PR from a new contributor would raise some eyebrows and require some discussion.

Once you guys are fully comfortable with this PR, I will re-sync, again fix all new errors, and move forward with submit.

It would really be best if the codebase had some method to prevent backsliding but I'm unaware of any short of enabling useDefineForClassFields.

To truly enable useDefineForClassFields, more changes will more than likely need to be made

Do you have a feel for what that would be?

When you actually enable the flag, the code emitted changes quite a bit more. You start using true class fields rather than object properties throughout. While testing with this flag enabled, I wasn't able to actually run VSCode due to some errors. I forget exactly what. Easiest way to test that out would be to patch this PR and turn on the flag to experiment.

Anyways, my point is that I would expect that someone would want to do rigorous testing once enabling this flag. This PR is safer as it only fixes all the compiler errors without actually making the change.

@mjbvz You initial concern in #186726 (comment) seems to have fixed, right? Any other insights this that you have?

Matt was correct that there were additional runtime errors to consider. In the case of moving only the initializers that reference parameter properties, there's still the issue of other non-constructor initializers that access other fields initialized in the constructor from a function. This can be safe if the function is evaluated after the constructor or unsafe if evaluated immediately (i.e. via waitForState or recomputeInitiallyAndOnChange). I found a few instances of this in relation to this PR which I fixed here (from observing errors and then from analyzing all instances of waitForState or recomputeInitiallyAndOnChange for safety (i.e. usage in a field initializer versus inside a method).

I can only be as confident as your tests though. I would hope that someone in the VSCode team could patch this PR and test things out a bit to check for anything that may not be as well covered in tests.

Beyond what I fixed, it's possible there are other code execution order changes that will be introduced via enabling useDefineForClassFields that need to be fixed.

@jrieken
Copy link
Member

jrieken commented Feb 25, 2025

but I'm unaware of any short of enabling useDefineForClassFields.

We can prevent erosion by adding another tsconfig which enables useDefineForClassFields and do a separate check with that. Will slow down CI by a tiny bit, for the time being, but something that we have done successfully in the past

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants